package com.mujin.keji.collectionsystem.module.mypager.views;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.module.login.dto.LoginDto;
import com.mujin.keji.collectionsystem.module.mypager.dto.UpdateUserDto;
import com.mujin.keji.collectionsystem.module.mypager.viewmodel.UserViewModel;
import com.mujin.keji.collectionsystem.utils.SPUtil;
import com.mujin.keji.collectionsystem.utils.ToastUtils;


/**
 * 修改手机号码界面
 *
 * @author weishixiong
 * @Time 2018-05-22
 */

public class UpdatePhoneActivity extends BaseActivity implements View.OnClickListener {
    private EditText tvPhone;
    private TextView tvUpdate;
    private UserViewModel userViewModel;

    @Override
    protected void createViewModel() {
        userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_update_phone;
    }

    @Override
    public void initView() {
        tvPhone = (EditText) findViewById(R.id.tv_phone);
        tvUpdate = (TextView) findViewById(R.id.tv_update);
        setCenterTitle("更改手机号", "#FFFFFF");
        setleftImage(R.drawable.image_arrow_left_white, true, null);

    }

    @Override
    public void initData() {
        tvPhone.setText(getIntent().getStringExtra("phone"));
        if(TextUtils.isEmpty(tvPhone.getText().toString())){
            tvUpdate.setEnabled(false);
            tvUpdate.setTextColor(Color.parseColor("#aaFFFFFF"));
        }
        else{
            tvUpdate.setEnabled(true);
            tvUpdate.setTextColor(Color.parseColor("#FFFFFF"));
        }
    }

    @Override
    public void initListener() {
        tvUpdate.setOnClickListener(this);
        tvPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(s.toString())){
                    tvUpdate.setEnabled(false);
                    tvUpdate.setTextColor(Color.parseColor("#aaFFFFFF"));
                }
                else{
                    tvUpdate.setEnabled(true);
                    tvUpdate.setTextColor(Color.parseColor("#FFFFFF"));
                }
            }
        });
    }

    private void update() {
        final LoginDto dto = SPUtil.getData(Constant.SP.SP, Constant.SP.LOGIN_KEY, LoginDto.class, null);
        getLoadingDialog().showDialog();
        userViewModel.getLiveData(dto == null ? "" : dto.getSex(),tvPhone.getText().toString()).observe(this, new Observer<BaseDto<UpdateUserDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<UpdateUserDto> updateUserDtoBaseDto) {
                ToastUtils.showToast(updateUserDtoBaseDto.getMsg());
                getLoadingDialog().dismiss();
                if(updateUserDtoBaseDto.getCode()==Constant.Server.SUCCESS_CODE){
                    dto.setTelephone(tvPhone.getText().toString());
                    SPUtil.putData(Constant.SP.SP, Constant.SP.LOGIN_KEY, dto);
                    finish();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_update:
                if (TextUtils.isEmpty(tvPhone.getText().toString()) || tvPhone.getText().toString().length() < 11) {
                    ToastUtils.showToast("请输入正确的手机号码");
                    return;
                }
                else if(!tvPhone.getText().toString().startsWith("1")){
                    ToastUtils.showToast("请输入正确的手机号码");
                    return;
                }
                update();
                break;
        }
    }
}
